package com.nova.bluetooth.manage;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.nova.bluetooth.bean.DeviceConfigBean;
import com.tencent.mmkv.MMKV;

import java.util.List;

public class MMKVManager {
    private MMKV mmkv = MMKV.mmkvWithID("sanag_ble", MMKV.MULTI_PROCESS_MODE); // 多线程使用
    private static final String KEY_USER_LIST = "device_config_list";
    public static MMKVManager getInstance() {
        return LazyHolder.instance;
    }

    private static class LazyHolder {
        private static final MMKVManager instance = new MMKVManager();
    }


    public static void saveDeviceConfigBeanList(List<DeviceConfigBean> userList) {
        MMKV mmkv = MMKV.defaultMMKV();
        Gson gson = new Gson();
        String json = gson.toJson(userList);
        mmkv.encode(KEY_USER_LIST, json);
    }

    public static List<DeviceConfigBean> getDeviceConfigBeanList() {
        MMKV mmkv = MMKV.defaultMMKV();
        String json = mmkv.decodeString(KEY_USER_LIST, null);
        if (json == null) {
            return null;
        }
        Gson gson = new Gson();
        return gson.fromJson(json, new TypeToken<List<DeviceConfigBean>>() {}.getType());
    }

    public void clearInfo() {
        mmkv.clearAll();
    }
}
